<?php 
session_start();
$req_path="./";
include_once($req_path."include/ini.php");
require_once($req_path."include/db.php");


class M_admin extends DB{
    private $dbConn;
    private $totalpage="0";
    private $rowCount="0";
    
    function M_admin(){
        parent::__construct();
    }
        
    function getRowCount(){
        return $this->rowCount;
    }
    
    function getTotalPage(){
        return $this->totalpage;
    }    
    function getMovieList($id){
        
        $sql = "SELECT movie_list FROM tb_orders WHERE id = '".$id."'  ";
        $rs = $this->ExecuteData($sql);
        $my_item="";
        
        if ($row = $rs->FetchRow()) {
            $my_item = '\''.str_replace(',', '\',\'', $row["movie_list"]).'\'';
        }
        
        
        return $this->getFileName($my_item) ;
    }
    function getFileName($list){
        
      
        $this->ExecuteData("SET NAMES ANSI");
        $sql = "SELECT DISTINCT directoryfullname FROM tb_files2 WHERE id IN (".$list.") ";
        
        $resultsub = $this->ExecuteData($sql);
        while (($rowsub = $resultsub->FetchRow()) ) {
          
            $out .= $rowsub["directoryfullname"];
 
             $out .=";";
    
        }
        
        return $out;
    }
    function checkUserLogin($name, $password){
        $sqlscript= "SELECT * FROM tb_users Where ID = 0 ";
        $sqlscript.= " and username='". $name."'";
        $sqlscript.= " and password ='". $password."'";
        //print $sqlscript;
        $result = $this->ExecuteData($sqlscript);
        $rows =$result->RecordCount();

        //Set Row
        $this->rowCount=$rows;
        
        if ($rows > 0){
            //Set result
            $this->reslt_val=$result->FetchRow();
            return array('val'=>1, 'result'=>$this->reslt_val); // Completed
        }else{
         
            return array('val'=>2,'result'=>'');
 
        }

        ///return $result;
    }
    function getNewsDetail($id){
      $sql=" select id,title, description from tb_news where delete_flage=0 and id=".$id    ;
      $result = $this->ExecuteData($sql);
       $rowsub = $result->FetchRow();
       return $rowsub;
        
    }
    function editNews($id,$title,$desc){
        $sql= "Update tb_news set title='".$title."' , description='".$desc."' where id=".$id;
         $result = $this->ExecuteData($sql);
        
    }
     function addNews($title,$desc){
         
         $sql = " Insert into tb_news (title, description, delete_flage , created_date)";
         $sql .= " values ('".$title."', '".$desc."' ,0 , NOW())";
         
          $result = $this->ExecuteData($sql);
         
     }
    function delNews($id){
        
            $sql =" UPDATE tb_news Set delete_flage=1 WHERE id=".$id;
          $result = $this->ExecuteData($sql);
          return true;
    }
    function getNews($title, $orderfrom , $ordertto, $order="default" , $typeOrder="desc",$pageStart=1,$rowPerPage){
		 global $HOUR_INTERVAL;
        $sql=" select id,title, DATE_FORMAT( DATE_ADD(  created_date   , INTERVAL ". $HOUR_INTERVAL." HOUR )  ,'%d/%m/%Y') createdDate from tb_news where delete_flage=0"    ;
        
        if ($title!=''){
            $sql.=" AND title like '%".$title."%'";
        }
        
      if ($orderfrom!="" && $ordertto!=""){
            
            $sql .=" AND DATE_Format(created_date,'%Y/%m/%d') BETWEEN '".$orderfrom."' and '".$ordertto."'";
        }
            
        if ($order!='default'){
            $sql.= " ORDER BY ". $order ." " . $typeOrder;        
        }else{
            $sql.=" ORDER BY created_date DESC ";
        }
        
         $result = $this->ExecuteData($sql);
         $rows =$result->RecordCount();
        
        $sqlscript.= " limit ". ($pageStart-1)*$rowPerPage. ", " . $rowPerPage;
        $result = $this->ExecuteData($sql.$sqlscript);      
        
        $totalpage=1;
        if ($rows>0){
            $rt = $rows%$rowPerPage;
            
            if($rt!=0) {
                $totalpage = floor($rows/$rowPerPage)+1;
            }
            else {
                $totalpage = floor($rows/$rowPerPage);
            }
        }
        $this->rowCount=$rows;
        $this->totalpage=$totalpage;    
        return $result;
    }
    
    function getOrder($status, $orderfrom , $ordertto, $order="default" , $typeOrder="desc",$pageStart=1,$rowPerPage){
      global $HOUR_INTERVAL;      
	  $sql = "SELECT O.*,ifnull(U.email,'') email
        , case when status = 0 then 'รอชำระเงิน' when status=1 then 'รอจ่ายเงิน' when status=2 then 'ปิดการขาย' else 'ยกเลิก' end _status
        , DATE_FORMAT( DATE_ADD(  adddate, INTERVAL ".$HOUR_INTERVAL." HOUR ) ,'%d/%m/%Y %H:%i:%s') as CreatedDate FROM tb_orders O ";
        $sql .= " LEFT JOIN tb_users U ON O.user_id = U.id ";
        $sql .= " WHERE 1=1 ";
        
        if ($orderfrom!=""){
            
            $sql .=" AND DATE_Format(adddate,'%Y/%m/%d') BETWEEN '".$orderfrom."' and '".$ordertto."'";
        }
        
        if ($status=="4") 
            $sql .= " ";
        else if ($status!="") 
            $sql .= " AND O.Status = '".$status."' ";
        else
            $sql .= " AND O.Status = '0' ";
        
        if ($order!='default'){
            $sql.= " ORDER BY ". $order ." " . $typeOrder;        
        }else{
            $sql.=" ORDER BY adddate DESC , status asc";
        }
        $result = $this->ExecuteData($sql);
        $rows =$result->RecordCount();
        
        $sqlscript.= " limit ". ($pageStart-1)*$rowPerPage. ", " . $rowPerPage;
        $result = $this->ExecuteData($sql.$sqlscript);      
        
        $totalpage=1;
        if ($rows>0){
            $rt = $rows%$rowPerPage;
            
            if($rt!=0) {
                $totalpage = floor($rows/$rowPerPage)+1;
            }
            else {
                $totalpage = floor($rows/$rowPerPage);
            }
        }
        $this->rowCount=$rows;
        $this->totalpage=$totalpage;    
        return $result;
       
    }     
    function updateOrder($orderID, $status){
        
        $sql = "update tb_orders set status = ".$status." where id=".$orderID;
        $this->ExecuteData($sql);
        
    } 
}